package com.wzy.webflux.controller;

import com.wzy.webflux.enpty.User;
import com.wzy.webflux.repository.UserRepository;
import com.wzy.webflux.util.CheckUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {
    private final UserRepository repository;

    public UserController(UserRepository repository) {
        this.repository = repository;
    }

    @GetMapping("/")
    public Flux<User> getAll() {
        return repository.findAll();
    }

    @GetMapping(value = "/stream/all", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<User> streamGetAll() {
        return repository.findAll();
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping("/")
    public Mono<User> createUser(@Valid @RequestBody User user) {
        //新增修改 都是save ,有id就是修改,id为空就是新增
        user.setId(null);
        CheckUtil.checkNames(user.getName());
        return repository.save(user);
    }

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Mono<ResponseEntity<Void>> deleteUser(@PathVariable("id") String id) {
        return repository.findById(id)
                .flatMap(user -> repository.delete(user)
                        .then(Mono.just(new ResponseEntity<Void>(HttpStatus.OK))))
                .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 更新用户
     *
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    public Mono<ResponseEntity<User>> updateUser(@PathVariable("id") String id,
                                                 @Valid @RequestBody User user) {
        CheckUtil.checkNames(user.getName());
        return repository.findById(id).flatMap(u -> {
            u.setAge(user.getAge());
            u.setName(user.getName());
            return repository.save(u);
        }).map(u -> new ResponseEntity<User>(u, HttpStatus.OK))
                .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 根据id查找用户
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Mono<ResponseEntity<User>> findUserById(@PathVariable("id") String id) {
        return repository.findById(id).map(u -> new ResponseEntity<User>(u, HttpStatus.OK))
                .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 根据年龄段查找用户
     *
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/age/{start}/{end}")
    public Flux<User> findByAgeBetween(@PathVariable("start") int start, @PathVariable("end") int end) {
        return repository.findByAgeBetween(start, end);
    }

    @GetMapping(value = "/stream/age/{start}/{end}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<User> findByAgeBetween2(@PathVariable("start") int start, @PathVariable("end") int end) {
        return repository.findByAgeBetween(start, end);
    }

    /**
     * 单独查找年龄20-30的用户
     *
     * @return
     */
    @GetMapping("/age")
    public Flux<User> findUserByAgeStage() {
        return repository.findUserByAgeStage();
    }

    @GetMapping(value = "/stream/age", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<User> findUserByAgeStage2() {
        return repository.findUserByAgeStage();
    }
}
